package cn.com.yiqi.wsc.portal.controller.cash;

import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultStatusEnum;
import cn.com.yiqi.wsc.common.ResultUtil;
import cn.com.yiqi.wsc.dao.balance.criteria.BalanceCriteria;
import cn.com.yiqi.wsc.dao.balance.entity.BalanceFetchEntityDTO;
import cn.com.yiqi.wsc.entity.check.Check;
import cn.com.yiqi.wsc.portal.controller.BaseController;
import cn.com.yiqi.wsc.service.balance.BalanceService;
import cn.com.yiqi.wsc.service.balance.PortalBalanceService;
import cn.com.yiqi.wsc.service.outputExcel.OutPutService;
import cn.com.yiqi.wsc.service.user.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * Created by 苟宇恒 on 2017/2/23.
 * 提现管理
 */
@Controller
@RequestMapping("/cash")
public class CashController extends BaseController {

    private static final int OUTPUT_MAX_ROW = 500_000;

    @Autowired
    private PortalBalanceService portalBalanceService;

    @Autowired
    private BalanceService balanceService;


    @Autowired
    private OutPutService outPutService;

    @Autowired
    private UserService userService;

    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView("cash/index");
        this.getUserData(mv);
        return mv;
    }


    @RequestMapping("/list")
    @ResponseBody
    public Result pageList(@RequestBody BalanceCriteria criteria) {

        Result result = new Result();

        result.setStatus(ResultStatusEnum.操作成功.getCode());
        Result<PageInfo<BalanceFetchEntityDTO>> pageInfoResult = portalBalanceService.pageList(criteria);

        boolean success = pageInfoResult.getStatus() == ResultStatusEnum.操作成功.getCode();

        if (success) {

            PageInfo pageInfo = pageInfoResult.getReturnValue();
            result.putData("list", pageInfo.getList());
            result.putData("total", pageInfo.getTotal());

        }

        return result;

    }

    @RequestMapping("/check")
    @ResponseBody
    public Result check(@RequestBody Check check) {
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        result = portalBalanceService.check(check);

        return result;

    }

    @RequestMapping("/fetch")
    @ResponseBody
    public Result fetch(@RequestBody BalanceFetchEntityDTO balanceFetchEntityDTO) {
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());


        Long fetchId = balanceFetchEntityDTO.getId();
        if (fetchId == null) {
            return ResultUtil.paramError(result, "参数错误");
        }


        result = balanceService.passFetchRequest(fetchId);

        return result;

    }


    @RequestMapping("/outPut")
    public void outPut(HttpServletRequest request, HttpServletResponse response,
                       @RequestParam(name = "customerName", required = false) String customerName,
                       @RequestParam(name = "status", required = false) Integer status,
                       @RequestParam(name = "from", required = false)
                       @DateTimeFormat(pattern = "yyyy-MM-dd") Date from,
                       @RequestParam(name = "to", required = false)
                       @DateTimeFormat(pattern = "yyyy-MM-dd") Date to

    ) {

        BalanceCriteria criteria = new BalanceCriteria();

        criteria.setCustomerName(customerName);
        criteria.setStatus(status);
        criteria.setFrom(from);
        criteria.setTo(to);
        criteria.setPageSize(OUTPUT_MAX_ROW);

        Result<PageInfo<BalanceFetchEntityDTO>> pageInfoResult = portalBalanceService.pageList(criteria);
        String title = "余额提现";


        String[] hearders = new String[]{"昵称", "申请金额", "实付金额", "支付类型(1.微信)", "第三方账号", "申请时间", "状态", "备注"};//表头数组
        String[] fields = new String[]{"customerName", "fetchAmount", "realAmount", "payMode", "externalUid", "applyTime", "statusName", "memo"};

        String creator = userService.getCurrentUser().getRealName();
        try {

            List<BalanceFetchEntityDTO> list = pageInfoResult.getReturnValue().getList();

            outPutService.outPutExcel(list, title, hearders, fields, creator, request, response);
        } catch (Exception e) {
            e.printStackTrace();

        }

    }

}
